package com.devicemantra.coordinate.dataobjects;

import javax.jdo.annotations.*;

import java.util.*;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;


@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable ="true")
public class CDepartment 
{
	@PrimaryKey
    @Persistent  //(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key mDepartmentId;
		
	@Persistent 
	private String mDepartmentName;
	
	@Persistent 
	private String mDepartmentDesc;
	
	@Persistent(defaultFetchGroup="true")
	Logo mLogo;
	
	@Persistent(defaultFetchGroup="true")
	private ContactInfo mContactInfo;
	
	@Persistent(mappedBy = "mMappedDepartment",defaultFetchGroup="true")
	@Element(dependent = "true") 
	private List<SubDepartment> mSubDepartmentList = 
										new ArrayList<SubDepartment>();
	
	@Persistent(mappedBy = "mMappedDepartment",defaultFetchGroup="true")
	@Element(dependent = "true") 
	private List<CLocation> mLocationList = new ArrayList<CLocation>();
	
	@Persistent 
	private int mLocationIdCount = 1;
	
	
	public CDepartment(String pDepartmentName)
	{
		mDepartmentName = pDepartmentName;
		//mSubDepartmentList = new ArrayList<SubDepartment>();
		
		System.out.println("CREATED DEPARTMENT " + pDepartmentName);
	}
	
	public CDepartment(	String pNewDepartmentName,
						String pDeptDesc,
						Logo pLogo,
						ContactInfo pContactInfo)
	{
		mDepartmentName = pNewDepartmentName;
		mDepartmentDesc = pDeptDesc;
		mLogo = pLogo;
		mContactInfo = pContactInfo;
	}
	
	public String getDepartmentName()
	{
		return mDepartmentName; 
	}
	
	public void setDepartmentName(String pDeptName)
	{
		mDepartmentName = pDeptName; 
		
	}
	
	public void setDepartmentId(Key pDepartmentKey)
	{
		mDepartmentId = pDepartmentKey;
	}
	
	public Key getDepartmentId()
	{
		return mDepartmentId ;
	} 
	
	public int addSubDepartment(SubDepartment pSubDepartment)
	{
		/* First check whether this Sub department is already added */
			mSubDepartmentList.add(pSubDepartment);
			return 0;
				
	}
	
	
	public int addLocation(CLocation pLocation)
	{
		/* First check whether this Sub department is already added */
			pLocation.setLocationId(getLatestLocationId());
			mLocationList.add(pLocation);
			return 0;
				
	}
	
	public List<SubDepartment> getSubDepartments()
	{
		return mSubDepartmentList;
	} 
	
	public List<CLocation> getLocations()
	{
		return mLocationList;
	} 
	
	public int getNumOfSubDepartments()
	{
		return mSubDepartmentList.size();
	}
	
	public String getDescription()
	{
		return mDepartmentDesc;
	}
	
	public void setDescription(String pDescription)
	{
		mDepartmentDesc = pDescription;
	}
	
	public void setContact(ContactInfo pContactInfo)
	{
		mContactInfo = pContactInfo;
	}
	
	public ContactInfo getContactInfo()
	{
		return mContactInfo;
	}
	
	public void setLogo(Logo pLogo)
	{
		mLogo = pLogo;
	}
	
	public Logo getLogo()
	{
		return mLogo;
	}
	
	public String getLatestLocationId()
	{
		mLocationIdCount++;
		return mDepartmentName + "_" +  mLocationIdCount;
	}
	

}
